package com.eco.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.constants.Constants;
import com.eco.dao.ds1.AiFishMapper;
import com.eco.entity.AiFishEntity;
import com.eco.entity.AiFishTypeEntity;
import com.eco.entity.ChartDTO;
import com.eco.service.AiFishService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AiFishServiceImpl implements AiFishService {

    private final AiFishMapper aiFishMapper;

    private final StringRedisTemplate stringRedisTemplate;

    public AiFishServiceImpl(AiFishMapper aiFishMapper, StringRedisTemplate stringRedisTemplate) {
        this.aiFishMapper = aiFishMapper;
        this.stringRedisTemplate = stringRedisTemplate;
    }


    @Override
    public int updateByAiFishEntity(AiFishEntity aiFishEntity) {
        return this.aiFishMapper.updateByAiFishEntity(aiFishEntity);
    }


    @Override
    public IPage<AiFishEntity> findByDate(String startDate, String endDate, int pageNum, int pageSize, String direction, String category, Float lenMin, Float lenMax) {

        IPage<AiFishEntity> page = this.aiFishMapper.findByDates(new Page<>(pageNum, pageSize), startDate, endDate, direction, category, lenMin, lenMax);
        List<AiFishEntity> datas = page.getRecords();
        datas.forEach(x->{
            x.setSwim("right".equals(x.getSwim())?"上游":"下游");
            x.setFishType(stringRedisTemplate.opsForValue().get(Constants.FISH_TYPE_REDIS_KEY+x.getFishType()));
        });

        return page;
    }

    @Override
    public List<AiFishEntity> findByRecordDateTime(String startDate, String endDate) {
        List<AiFishEntity> datas = this.aiFishMapper.findByDatesTime(startDate, endDate);
        for (int i = 0; i < datas.size(); i++) {
            if (StringUtils.equals("right", datas.get(i).getSwim())) {
                datas.get(i).setSwim("0");
            } else {
                datas.get(i).setSwim("1");
            }
        }
        return datas;
    }

    @Override
    public void deleteById(int[] ids) {
        this.aiFishMapper.deleteById(ids);
    }

    @Override
    public List<AiFishTypeEntity> findAllFishType() {
        return this.aiFishMapper.findAllFishType();
    }

    @Override
    public List<ChartDTO> getFishCountByEveryday(String startDate, String endDate) {
        return this.aiFishMapper.getFishCountByEveryday(startDate, endDate);
    }

    @Override
    public int countByRecordDate(String startDate, String endDate, String direction, String category, Float lenMin, Float lenMax) {
        return this.aiFishMapper.countByRecordDate(startDate, endDate, direction, category, lenMin, lenMax);
    }

    @Override
    public int countAll(String swim) {
        return this.aiFishMapper.countAll(swim);
    }

    @Override
    public AiFishEntity findById(int id) {
        return this.aiFishMapper.findById(id);
    }
}
